<?php

require_once 'dbconf.php';

class ParserController {

    private $request;
    private $page = null;

    public function __construct() {
        $this->request = $_REQUEST;
    }

    private function updateAction() {
        Options::getSettings();
    }
    
    private function getLinksAction() {
        $link_start = isset($_REQUEST['link_start']) ? $_REQUEST['link_start'] : false;
        $link_end = isset($_REQUEST['link_end']) ? $_REQUEST['link_end'] : false;
        $paginator_start = isset($_REQUEST['paginator_start']) ? $_REQUEST['paginator_start'] : false;
        $paginator_end = isset($_REQUEST['paginator_end']) ? $_REQUEST['paginator_end'] : false;       
        $links = new Links($link_start, $link_end, $paginator_start, $paginator_end);
        $links->getCatLinks();
        $links->getTovarLinks();
    }   
    
    private function loadAction() {
        $site_url = isset($_REQUEST['url']) ? mysql_real_escape_string($_REQUEST['url']) : "";        
        $options = Options::getInstance($site_url);
        $options->loadSettings();
    }
    
    private function uploadAction() {
        Links::uploadCatFile();
    }
    
    private function viewTovarLinksAction() {
        Links::viewTovarLinks();
    }

    private function startAction() { 
        $site_url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false;
        $name_start = isset($_REQUEST['tovar_name1']) ? $_REQUEST['tovar_name1'] : false;
        $name_end = isset($_REQUEST['tovar_name2']) ? $_REQUEST['tovar_name2'] : false;
        $art_start = isset($_REQUEST['art1']) ? $_REQUEST['art1'] : false;
        $art_end = isset($_REQUEST['art2']) ? $_REQUEST['art2'] : false;
        $price_start = isset($_REQUEST['price1']) ? $_REQUEST['price1'] : false;
        $price_end = isset($_REQUEST['price2']) ? $_REQUEST['price2'] : false;
        $desc_start = isset($_REQUEST['desc_full1']) ? $_REQUEST['desc_full1'] : false;
        $desc_end = isset($_REQUEST['desc_full2']) ? $_REQUEST['desc_full2'] : false;
        $har_start = isset($_REQUEST['har1']) ? $_REQUEST['har1'] : false;
        $har_end = isset($_REQUEST['har2']) ? $_REQUEST['har2'] : false;
        $check = isset($_REQUEST['gran']) ? $_REQUEST['gran'] : 'off';
        $img_start = isset($_REQUEST['img1']) ? $_REQUEST['img1'] : false;
        $img_end = isset($_REQUEST['img2']) ? $_REQUEST['img2'] : false;
        $search = isset($_REQUEST['big_img1']) ? $_REQUEST['big_img1'] : false;
        $replace = isset($_REQUEST['big_img2']) ? $_REQUEST['big_img2'] : false;
        $options = Options::getInstance($site_url);
        $options->init($name_start, $name_end, $art_start, $art_end, $price_start, $price_end, $desc_start, $desc_end, $har_start, $har_end, $check, $img_start, $img_end, $search, $replace);
        $parser = new Parser($options);
        $parser->parse();
        Result::createZip($site_url);
    }

    private function indexAction() {
        $this->page = new Page('pages/index.html');
    }

    private function saveAction() {
        $site_url = isset($_REQUEST['url']) ? mysql_real_escape_string($_REQUEST['url']) : false;
        $name_start = isset($_REQUEST['tovar_name1']) ? mysql_real_escape_string($_REQUEST['tovar_name1']) : false;
        $name_end = isset($_REQUEST['tovar_name2']) ? mysql_real_escape_string($_REQUEST['tovar_name2']) : false;
        $art_start = isset($_REQUEST['art1']) ? mysql_real_escape_string($_REQUEST['art1']) : false;
        $art_end = isset($_REQUEST['art2']) ? mysql_real_escape_string($_REQUEST['art2']) : false;
        $price_start = isset($_REQUEST['price1']) ? mysql_real_escape_string($_REQUEST['price1']) : false;
        $price_end = isset($_REQUEST['price2']) ? mysql_real_escape_string($_REQUEST['price2']) : false;
        $desc_start = isset($_REQUEST['desc_full1']) ? mysql_real_escape_string($_REQUEST['desc_full1']) : false;
        $desc_end = isset($_REQUEST['desc_full2']) ? mysql_real_escape_string($_REQUEST['desc_full2']) : false;
        $har_start = isset($_REQUEST['har1']) ? mysql_real_escape_string($_REQUEST['har1']) : false;
        $har_end = isset($_REQUEST['har2']) ? mysql_real_escape_string($_REQUEST['har2']) : false;
        $check = isset($_REQUEST['gran']) ? mysql_real_escape_string($_REQUEST['gran']) : 'off';
        $img_start = isset($_REQUEST['img1']) ? mysql_real_escape_string($_REQUEST['img1']) : false;
        $img_end = isset($_REQUEST['img2']) ? mysql_real_escape_string($_REQUEST['img2']) : false;
        $search = isset($_REQUEST['search']) ? mysql_real_escape_string($_REQUEST['search']) : false;
        $replace = isset($_REQUEST['replace']) ? mysql_real_escape_string($_REQUEST['replace']) : false;
        $options = Options::getInstance($site_url);
        $options->init($name_start, $name_end, $art_start, $art_end, $price_start, $price_end, $desc_start, $desc_end, $har_start, $har_end, $check, $img_start, $img_end, $search, $replace);
        $options->SaveSettings();
    }

    public function run() {
        $action = isset($this->request['a']) ? $this->request['a'] : 'index';

        switch ($action) {
            case 'view_links':
                $this->viewTovarLinksAction();
                break;
            case 'links':
                $this->getLinksAction();
                break;
            case 'load':
                $this->loadAction();
                break;
            case 'update':
                $this->updateAction();
                break;
            case 'save':
                $this->saveAction();
                break;
            case 'start':
                $this->startAction();
                break;
            case 'index':
                $this->indexAction();
                break;
            case 'upload':
                $this->uploadAction();
                break;
            default:
                break;
        }

        if (!is_null($this->page)) {
            $this->page->show();
        }
    }

}

?>
